package com.ruoyi.web.controller.car;

import java.util.List;

import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.framework.util.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.car.domain.CarCompany;
import com.ruoyi.car.service.ICarCompanyService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 车辆所属车企Controller
 * 
 * @author zls
 * @date 2020-03-29
 */
@Controller
@RequestMapping("/car/company")
public class CarCompanyController extends BaseController
{
    private String prefix = "car/company";

    @Autowired
    private ICarCompanyService carCompanyService;

    @RequiresPermissions("car:company:view")
    @GetMapping()
    public String company()
    {
        return prefix + "/company";
    }

    /**
     * 查询车辆所属车企列表
     */
    @RequiresPermissions("car:company:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(CarCompany carCompany)
    {
        startPage();
        List<CarCompany> list = carCompanyService.selectCarCompanyList(carCompany);
        return getDataTable(list);
    }

    /**
     * 导出车辆所属车企列表
     */
    @RequiresPermissions("car:company:export")
    @Log(title = "车辆所属车企", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(CarCompany carCompany)
    {
        List<CarCompany> list = carCompanyService.selectCarCompanyList(carCompany);
        ExcelUtil<CarCompany> util = new ExcelUtil<CarCompany>(CarCompany.class);
        return util.exportExcel(list, "company");
    }

    /**
     * 新增车辆所属车企
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存车辆所属车企
     */
    @RequiresPermissions("car:company:add")
    @Log(title = "车辆所属车企", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(MultipartFile file,CarCompany carCompany) {
        String operName = ShiroUtils.getSysUser().getLoginName();
        carCompany.setCreateBy(operName);
        carCompany.setUpdateBy(operName);
        try {
            String upload = FileUploadUtils.upload(file);
            carCompany.setPicSmall(upload);
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
        return toAjax(carCompanyService.insertCarCompany(carCompany));
    }

    /**
     * 修改车辆所属车企
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        CarCompany carCompany = carCompanyService.selectCarCompanyById(id);
        mmap.put("carCompany", carCompany);
        return prefix + "/edit";
    }

    /**
     * 修改保存车辆所属车企
     */
    @RequiresPermissions("car:company:edit")
    @Log(title = "车辆所属车企", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(CarCompany carCompany) {
        return toAjax(carCompanyService.updateCarCompany(carCompany));
    }

    /**
     * 删除车辆所属车企
     */
    @RequiresPermissions("car:company:remove")
    @Log(title = "车辆所属车企", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(carCompanyService.deleteCarCompanyByIds(ids));
    }
}
